package mulegame;

import javax.swing.JPanel;
import javax.swing.border.MatteBorder;

import java.awt.Color;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;

import java.awt.Font;

import javax.swing.border.BevelBorder;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.border.LineBorder;

import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.border.TitledBorder;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
/**
 * Makes Main Screen, JPanel, controls turns and auction and defines mapbutton
 * 
 * @author AREA 29
 *
 */
public class MainScreen extends TimedScreen {
    private Map map;
    private MapButton[] buttonList;
    private boolean mapChanged = true;
    private JLabel lblTimer;
    private JButton btnBuy;
    private JButton btnPass; 
    private int timeleft;
    private MapButton selectedTile = null; 
    
    
    /**
     * sets internal objects ctrl, duration, and map to specified parameters
     * calculates time left in turn
     * check to see if map tiles are created before panel is displayed
     * 
     * @param ctrl graphics control object
     * @param duration what turn time is based on 
     * @param map map object
     */
    public MainScreen(GraphicsControl ctrl, int duration, Map map){
           super(ctrl, duration);
           this.map = map;
           timeleft = (duration/1000);
          
            setSize(new Dimension(900, 600));
            setBorder(new MatteBorder(4, 4, 4, 4, (Color) new Color(0, 0, 0)));
            setLayout(null);
            
    		setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    
            JLabel lblMainTitle = new JLabel("Coffee Mule");
            lblMainTitle.setBounds(175, 11, 563, 51);
            lblMainTitle.setForeground(new Color(165, 42, 42));
            lblMainTitle.setHorizontalAlignment(SwingConstants.CENTER);
            lblMainTitle.setFont(new Font("Batang", Font.BOLD, 45));
            add(lblMainTitle);
    
            JLabel lblLogoSMALL = new JLabel("");
            lblLogoSMALL.setBounds(20, 11, 97, 93);
            lblLogoSMALL.setBorder(new LineBorder(new Color(0, 0, 0), 2));
            lblLogoSMALL
                    .setIcon(new ImageIcon("src/mulegame/Images/LogoSmall.JPG"));
            add(lblLogoSMALL);
    
            lblTimer = new JLabel("XX Seconds");
            lblTimer.setForeground(Color.RED);
            lblTimer.setBorder(new LineBorder(Color.BLACK, 2));
            lblTimer.setFont(new Font("Times New Roman", Font.PLAIN, 29));
            lblTimer.setHorizontalAlignment(SwingConstants.CENTER);
            lblTimer.setBounds(708, 10, 168, 83);
            add(lblTimer);
    
            btnBuy = new JButton(new BuyButtonAction());
            btnBuy.setText("BUY");
            btnBuy.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null,
                    null));
            btnBuy.setFont(new Font("Times New Roman", Font.PLAIN, 29));
            btnBuy.setBorderPainted(true);
            btnBuy.setBounds(30, 490, 168, 43);
            add(btnBuy); 
            
            btnPass = new JButton(new PassAction());
            btnPass.setText("PASS");
            btnPass.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null,
                    null));
            btnPass.setFont(new Font("Times New Roman", Font.PLAIN, 29));
            btnPass.setBorderPainted(true);
            btnPass.setBounds(30, 540, 168, 43);
            add(btnPass);
    
            JButton btnContinue = new JButton(new ContinueAction());
            btnContinue.setText("CONTINUE");
            btnContinue.setBorder(new BevelBorder(BevelBorder.RAISED, null, null,
                    null, null));
            btnContinue.setFont(new Font("Times New Roman", Font.PLAIN, 29));
            btnContinue.setBorderPainted(true);
            btnContinue.setBounds(697, 500, 168, 83);
            add(btnContinue);
    
            JPanel mapBorder = new JPanel();
            mapBorder.setBorder(new TitledBorder(null, "Coffee World",
                    TitledBorder.LEADING, TitledBorder.TOP, null, null));
            mapBorder.setBounds(223, 83, 471, 295);
            add(mapBorder);
            mapBorder.setLayout(null);
    
            JPanel theMap = new JPanel();
            theMap.setBounds(10, 27, 450, 250);
            mapBorder.add(theMap);
            theMap.setLayout(new GridLayout(5, 9, 5, 5));
    
            JPanel panelKey = new JPanel();
            panelKey.setBorder(new TitledBorder(null, "Property/Mule Key",
                    TitledBorder.LEADING, TitledBorder.TOP, null, null));
            panelKey.setBounds(697, 116, 181, 262);
            add(panelKey);
            panelKey.setLayout(null);
    
            JLabel lblProperty = new JLabel("Owned : Not");
            lblProperty.setBounds(6, 20, 169, 38);
            panelKey.add(lblProperty);
            lblProperty.setHorizontalAlignment(SwingConstants.CENTER);
            lblProperty.setForeground(Color.BLACK);
            lblProperty.setFont(new Font("MV Boli", Font.BOLD, 18));
    
            JLabel lblMule = new JLabel("Parked : Not");
            lblMule.setBounds(6, 130, 169, 38);
            panelKey.add(lblMule);
            lblMule.setHorizontalAlignment(SwingConstants.CENTER);
            lblMule.setForeground(Color.BLACK);
            lblMule.setFont(new Font("MV Boli", Font.BOLD, 18));
    
            JButton btnOwned = new JButton("");
            btnOwned.setIcon(new ImageIcon("src/mulegame/Images/P1F.jpg"));
            btnOwned.setBorder(new LineBorder(Color.BLUE, 5));
            btnOwned.setBorderPainted(true);
            btnOwned.setBounds(29, 69, 50, 50);
            panelKey.add(btnOwned);
    
            JButton btnNotOwned = new JButton("");
            btnNotOwned.setIcon(new ImageIcon("src/mulegame/Images/P1F.jpg"));
            btnNotOwned.setBorder(new LineBorder(Color.BLACK, 2));
            btnNotOwned.setBorderPainted(true);
            btnNotOwned.setBounds(108, 69, 50, 50);
            panelKey.add(btnNotOwned);
    
            JButton btnParked = new JButton("");
            btnParked.setIcon(new ImageIcon("src/mulegame/Images/P1FT.jpg"));
            btnParked.setBorder(new LineBorder(Color.BLACK, 2));
            btnParked.setBorderPainted(true);
            btnParked.setBounds(29, 179, 50, 50);
            panelKey.add(btnParked);
    
            JButton btnNotParked = new JButton("");
            btnNotParked.setIcon(new ImageIcon("src/mulegame/Images/P1F.jpg"));
            btnNotParked.setBorder(new LineBorder(Color.BLACK, 2));
            btnNotParked.setBorderPainted(true);
            btnNotParked.setBounds(108, 179, 50, 50);
            panelKey.add(btnNotParked);
    
            buttonList = new MapButton[9 * 5];
            MapButtonAction buttonListener = new MapButtonAction();
            MapButton btn;
            int listIndex = 0;
            for (int i = 0; i < 5; i++) {
                for (int k = 0; k < 9; k++) {
                    btn = new MapButton(buttonListener);
                    buttonList[listIndex++] = btn;
                    theMap.add(btn);
                }
            }
            MapButton town = buttonList[listIndex/2];
            town.removeActionListener(buttonListener);
            town.setAction(new TownAction());
            
           timerTick(); //this call to ensure the map tiles are created before the panel is displayed
           mapChanged = false;

    }
	
    /**
     * returns time left in turn
     * @return timeleft
     */
    public int getTimeLeft(){
	     return timeleft;   
	}

	@Override
	/**
	 * resets timer based on whether the player made an action on the map and
	 * updates info on the map.
	 */
	public void timerTick(){
	    if(mapChanged){
	        Tile[][] tiles = map.getTileGrid();
	        int listIndex = 0;
	        MapButton btn;
	        Color col;
	        int border;
	        for(Tile[] row: tiles){
	            for(Tile current: row){
	                btn = buttonList[listIndex++];
	                btn.setTile(current);
	            }
	        }
	        mapChanged = false;
	    }
	    if(selectedTile != null){
	       selectedTile.flash();
	    }
	    
	    Integer time = timeleft--;
	    lblTimer.setText(time.toString() + " Seconds");
        invalidate();//this should be the last call in this method
	    
	}

	@Override
	/**
	 * When the player runs out of time, the control object calls the
	 * finishMainScreen method.
	 */
	public void timerTimeOut() {
		control.finishMainScreen();
	}
	
	/**
	 * enables buy button and gives price 
	 * @param cost cost of what's bought
	 */
	public void setBuyButtonEnabled(int cost){
	    if (selectedTile != null){
	         btnBuy.setEnabled(true); 
	         btnBuy.setText("BUY [$" + Integer.toString(cost) + "]");
	    }
	}
	
	/**
	 * disables buy button
	 */
	public void setBuyButtonDisabled(){
	     btnBuy.setEnabled(false);
	     btnBuy.setText("BUY");
	}
	
	/**
	 * enables pass button 
	 * @param en boolean the can enable a button
	 */
	public void setPassButtonEnable(boolean en){
	     btnPass.setEnabled(en);
	}
	
	/**
	 * returns which tile has been selected
	 * @return selected tile
	 */
	public Tile getSelectedTile(){
	     return selectedTile.getTile();   
	}
	

	/**
	 * Defines what MapButton does and creates a MapButton object. The control
	 * object also determines which tile has been selected.
	 */
    private class MapButtonAction extends AbstractAction{
        @Override
        public void actionPerformed(ActionEvent evnt){
            MapButton src = (MapButton)evnt.getSource();
            if(selectedTile != null){
                selectedTile.reset();   
            }
            selectedTile = src;
            control.tileSelected(src.getTile());
            mapChanged = true;
        }
    }	
    
    /**
     * creates action listener for buy button
     * @author AREA 29
     *
     */
    private class BuyButtonAction extends AbstractAction{
        @Override
        public void actionPerformed(ActionEvent evnt){
            if (selectedTile != null){
                control.buyClicked();
                mapChanged = true;
            }
        }
    }
    
    /**
     * creates button for town button
     * @author AREA 29
     *
     */
    private class TownAction extends AbstractAction{
        @Override
        public void actionPerformed(ActionEvent evnt){
            control.townClicked();
        }
    }
    
    /**
     * creates action listener for pass button
     * @author AREA 29
     *
     */
    private class PassAction extends AbstractAction{
        @Override
        public void actionPerformed(ActionEvent evnt){
            control.passClicked();   
        }
    }
    
    /**
     * creates action listener for continue button
     * @author AREA 29
     *
     */
    private class ContinueAction extends AbstractAction{
        @Override
        public void actionPerformed(ActionEvent evnt){
            control.continueClicked();
        }
    }
}
